/*
 * Copyright 1999-2018 Alibaba Group Holding Ltd.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.mz.mattress.client.interceptor;

import com.mz.mattress.api.common.NameThreadFactory;

import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

public class GlobalExecutor {

    public static final ThreadPoolExecutor COMMON_EXECUTOR = new ThreadPoolExecutor(2, 2,
            60L, TimeUnit.SECONDS, new LinkedBlockingQueue<>(), new NameThreadFactory("thread-common-")
    );

    public static final ThreadPoolExecutor SINGLE_EXECUTOR = new ThreadPoolExecutor(1, 1,
            60L, TimeUnit.SECONDS, new LinkedBlockingQueue<>(), new NameThreadFactory("thread-single-")
    );

//    public static final ThreadPoolExecutor COMMON_EXECUTOR = new ThreadPoolExecutor(2, 2,
//            60L, TimeUnit.SECONDS, new LinkedBlockingQueue<>(), r -> {
//        Thread thread = new Thread(r, "thread-common");
//        thread.setDaemon(true);
//        return thread;
//    });


    public static void executeByCommon(Runnable runnable) {
        if (COMMON_EXECUTOR.isShutdown()) {
            return;
        }
        COMMON_EXECUTOR.execute(runnable);
    }

    public static void executeBySingle(Runnable runnable) {
        if (SINGLE_EXECUTOR.isShutdown()) {
            return;
        }
        SINGLE_EXECUTOR.execute(runnable);
    }


}
